/* This file is part of SurfBoard.

   SurfBoard is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   SurfBoard is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with SurfBoard.  If not, see <http://www.gnu.org/licenses/>. */

#import "SBAddressBarCell.h"

@implementation SBAddressBarCell
- (NSImage *)icon {
    return icon;
}
- (void)setIcon:(NSImage *)newValue {
    if (icon && newValue)
        [[self controlView] setNeedsDisplayInRect:NSMakeRect(3, 3, 16, 16)];
    else if (icon || newValue)
        [[self controlView] setNeedsDisplay:YES];
    [icon release];
    icon = [newValue retain];
    [icon setFlipped:YES];
}
- (CGFloat)progress {
    return progress;
}
- (void)setProgress:(CGFloat)newValue {
    progress = newValue;
    [[self controlView] setNeedsDisplay:YES];
}
- (void)dealloc {
    [icon release];
    [super dealloc];
}
- (BOOL)drawsBackground {
    return NO;
}
- (void)drawWithFrame:(NSRect)frame inView:(NSView *)view {
    NSRect progressRect, whiteRect = NSInsetRect(frame, 1, 1);
    // Only show the integrated progress bar if we aren't selected
    if ([[[self controlView] window] firstResponder] != [self controlView])
        NSDivideRect(NSInsetRect(frame, 1, 1), &progressRect, &whiteRect, (NSWidth(frame) - 2) * progress, NSMinYEdge);
    [[NSColor whiteColor] set];
    NSRectFill(whiteRect);
    if (!NSIsEmptyRect(progressRect)) {
        NSColor *lightBlue = [NSColor colorWithCalibratedRed:0.765 green:0.886 blue:0.988 alpha:1];
        [[[[NSGradient alloc] initWithColors:[NSArray arrayWithObjects:lightBlue, [NSColor colorWithCalibratedRed:0.686
                green:0.824 blue:0.996 alpha:1], lightBlue, nil]] autorelease] drawInRect:progressRect angle:90];
    }
    // The border color changes depending on whether the window is in focus.
    [[NSColor colorWithCalibratedWhite:([[[self controlView] window] isKeyWindow] ? 0.31 : 0.62) alpha:1] set];
    // The address bar has rounded corners, à la Safari 4.
    [[NSBezierPath bezierPathWithRoundedRect:NSInsetRect(frame, 0.5, 0.5) xRadius:3 yRadius:3] stroke];
    [[NSColor colorWithCalibratedWhite:0.886 alpha:1] set];
    [NSBezierPath strokeLineFromPoint:NSMakePoint(2, 1.5) toPoint:NSMakePoint(NSWidth(frame) - 2, 1.5)];
    // Expand the clipping path for the focus ring.
    [[NSBezierPath bezierPathWithRoundedRect:NSMakeRect(NSMinX(frame) - 4, NSMinY(frame) - 4, NSWidth(frame) + 8,
                                                        NSHeight(frame) + 8) xRadius:3 yRadius:3] setClip];
    if ([self showsFirstResponder]) {
        [NSGraphicsContext saveGraphicsState];
        NSSetFocusRingStyle(NSFocusRingOnly);
        // Draw focus ring.
        [[NSBezierPath bezierPathWithRoundedRect:frame xRadius:3 yRadius:3] fill];
        [NSGraphicsContext restoreGraphicsState];
    } else {
        [[NSColor colorWithCalibratedWhite:([[[self controlView] window] isKeyWindow] ? 0.749 : 0.886) alpha:1] set];
        NSBezierPath *path = [NSBezierPath bezierPath];
        [path moveToPoint:NSMakePoint(0.5, NSHeight(frame) - 2)];
        // This is probably an obfusticated way to do this
        [path appendBezierPathWithArcWithCenter:NSMakePoint(2, NSHeight(frame) - 2) radius:2 startAngle:90 endAngle:90
                                      clockwise:YES];
        [path lineToPoint:NSMakePoint(NSWidth(frame) - 2, NSHeight(frame) + 0.5)];
        [path appendBezierPathWithArcWithCenter:NSMakePoint(NSWidth(frame) - 2, NSHeight(frame) - 2) radius:2
                                     startAngle:90 endAngle:20 clockwise:YES];
        [path stroke];
    }
    [self drawInteriorWithFrame:frame inView:view];
}
- (void)drawInteriorWithFrame:(NSRect)frame inView:(NSView *)view {
    if (icon) {
        [icon drawInRect:NSMakeRect(3, 3, 16, 16) fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
        frame.origin.x += 18;
        frame.size.width -= 18;
    }
    [super drawInteriorWithFrame:frame inView:view];
}
- (void)editWithFrame:(NSRect)rect inView:(NSView *)view editor:(NSText *)txt delegate:(id)obj event:(NSEvent *)event {
    if (icon) {
        rect.origin.x += 18;
        rect.size.width -= 18;
    }
    [super editWithFrame:rect inView:view editor:txt delegate:obj event:event];
}
- (void)selectWithFrame:(NSRect)rect inView:(NSView *)view editor:(NSText *)txt delegate:(id)obj start:(NSInteger)start
                 length:(NSInteger)len {
    if (icon) {
        rect.origin.x += 18;
        rect.size.width -= 18;
    }
    [super selectWithFrame:rect inView:view editor:txt delegate:obj start:start length:len];
}
@end